﻿using System;
using System.Threading;
using System.Windows.Input;

namespace NanoMvvm
{
    public abstract class CommandWrapperBase<T> : CommandBase where T : CommandBase
    {
        protected CommandWrapperBase(T command)
        {
            WrappedCommand = command;
        }

        protected T WrappedCommand { get; private set; }

        private ICommand WrappedICommand { get { return WrappedCommand; } }

        protected override void Execute(object parameter)
        {
            WrappedICommand.Execute(parameter);
        }

        protected override bool CanExecute(object parameter)
        {
            return WrappedICommand.CanExecute(parameter);
        }

        protected override void OnCanExecuteChangedAddEventHandler(EventHandler handler)
        {
            WrappedICommand.CanExecuteChanged += handler;
        }

        protected override void OnCanExecuteChangedRemoveEventHandler(EventHandler handler)
        {
            WrappedICommand.CanExecuteChanged -= handler;
        }

        public override SynchronizationContext SynchronizationContext
        {
            get
            {
                return WrappedCommand.SynchronizationContext;
            }
        }

        public override bool IsAutomaticRequeryDisabled
        {
            get
            {
                return WrappedCommand.IsAutomaticRequeryDisabled;
            }
            set
            {
                WrappedCommand.IsAutomaticRequeryDisabled = value;
            }
        }


        protected internal override void OnCanExecuteChanged(ICommand sender)
        {
            WrappedCommand.OnCanExecuteChanged(this);
        }
    }
}